
Imports System
Imports System.Collections
Imports System.Data.SqlClient
Imports System.Diagnostics
Imports _2bMatesSite.Common

Namespace _2bMatesSite.BO
	''' <summary>
	''' Summary description for SpecialGroup.
	''' </summary>
	Public Class SpecialGroup
		Public ID As Integer
		Protected name As String
		Protected specialPageID As Integer
		Protected specialPages As ArrayList

		Public Sub New(ByVal ID As Integer, ByVal Name As String)
			Me.ID = ID
			Me.name = Name
			Me.specialPageID = -1
			Me.specialPages = New ArrayList()
		End Sub

		Public Property Name() As String
			Get
				Return name
			End Get

			Set
				name = value
			End Set
		End Property

		Public Property SpecialPageID() As Integer
			Get
				Return specialPageID
			End Get

			Set
				specialPageID = value
			End Set
		End Property

		Public Overridable Property SpecialPages() As ArrayList
			Get
				Return specialPages
			End Get
			Set
				specialPages = value
			End Set
		End Property

		Public Function LoadGroupSpecialPages() As ArrayList
			Dim specialGroupPagesIDs As Hashtable = GetGroupsSpecialPages()
			Dim list As New ArrayList()

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				Dim cmd As New SqlCommand("SELECT * FROM SpecialPages ORDER BY Name ASC")
				cmd.Connection = conn

				Dim dr As SqlDataReader = cmd.ExecuteReader()

				Dim ordID As Integer = dr.GetOrdinal("ID")
				Dim ordName As Integer = dr.GetOrdinal("Name")

				While dr.Read()
					Dim usp As New GroupSpecialPage(dr.GetInt32(ordID), dr.GetString(ordName))
					If specialGroupPagesIDs.Contains(usp.spID) Then
						usp.IsMember = True
					Else
						usp.IsMember = False
					End If
					list.Add(usp)
				End While

				dr.Close()
			Finally
				conn.Close()
			End Try
			Return list
		End Function

		Protected Function GetGroupsSpecialPages() As Hashtable
			Dim usp As New Hashtable()

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				Dim cmd As New SqlCommand("SELECT SpecialPageID FROM SpecialGroups WHERE GroupID=@ID")
				cmd.Connection = conn
				cmd.Parameters.Add("@ID", ID)

				Dim dr As SqlDataReader = cmd.ExecuteReader()

				While dr.Read()
					usp(dr.GetInt32(dr.GetOrdinal("SpecialPageID"))) = dr.GetInt32(dr.GetOrdinal("SpecialPageID"))
				End While
				dr.Close()
			Finally
				conn.Close()
			End Try

			Return usp
		End Function

		Public Class GroupSpecialPage
			Public spID As Integer
			Protected name As String
			Protected isMember As Boolean

			Public Sub New()
				spID = -1
				name = ""
				isMember = False
			End Sub

			Public Sub New(ByVal ID As Integer, ByVal name As String)
				Me.spID = ID
				Me.name = name
			End Sub

			Public Property Name() As String
				Get
					Return name
				End Get

				Set
					name = value
				End Set
			End Property

			Public Property IsMember() As Boolean
				Get
					Return isMember
				End Get

				Set
					isMember = value
				End Set
			End Property
		End Class

		Public Sub Save()
			DoInsert()
		End Sub

		Private Sub DoInsert()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Dim tran As SqlTransaction = conn.BeginTransaction()

			Try
				' Deelte all associated to this group special pages
				Dim cmd As New SqlCommand(GetDeleteSQL())
				cmd.Connection = conn
				cmd.Transaction = tran

				cmd.Parameters.Add("@GroupID", ID)

				Dim rows As Integer = cmd.ExecuteNonQuery()
				Debug.Assert(rows = 1)

				Dim specialPagesCount As Integer = specialPages.Count
				Dim i As Integer

				i = 0
				While i < specialPagesCount
					Dim tmpSpecialPage As GroupSpecialPage = DirectCast(specialPages(i), GroupSpecialPage)

					If tmpSpecialPage.IsMember Then
						cmd = New SqlCommand(GetInsertSQL())
						cmd.Connection = conn
						cmd.Transaction = tran

						cmd.Parameters.Add("@GroupID", ID)
						cmd.Parameters.Add("@SpecialPageID", tmpSpecialPage.spID)

						rows = cmd.ExecuteNonQuery()
						Debug.Assert(rows = 1)
					End If
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				tran.Commit()
			Catch e As Exception
				tran.Rollback()
				Throw e
			Finally
				conn.Close()
			End Try
		End Sub

		Private Shared Function GetInsertSQL() As String
			Dim sql As String = "INSERT INTO SpecialGroups" + Environment.NewLine
			sql += "( GroupID, SpecialPageID )" + Environment.NewLine
			sql += "VALUES" + Environment.NewLine
			sql += "( @GroupID, @SpecialPageID )" + Environment.NewLine
			Return sql
		End Function

		Private Shared Function GetDeleteSQL() As String
			Dim sql As String = "DELETE FROM SpecialGroups" + Environment.NewLine
			sql += "WHERE" + Environment.NewLine
			sql += "" & Chr(9) & "GroupID=@GroupID" + Environment.NewLine
			Return sql
		End Function

		Private Sub PopulateCommand(ByVal cmd As SqlCommand)
			DBManager.SetNZValue(cmd, "@GroupID", ID)
			DBManager.SetNZValue(cmd, "@SpecialPageID", specialPageID)
		End Sub

		Public Shared Function IsMemeber(ByVal id As Integer, ByVal specialPageID As Integer) As Boolean
			Dim tempSpecialGroup As SpecialGroup = GetSpecialGroupByPageID(id, specialPageID)
			If tempSpecialGroup <> Nothing Then
				Return True
			Else
				Return False
			End If
		End Function

		Public Shared Function GetSpecialGroupByPageID(ByVal id As Integer, ByVal specialPageID As Integer) As SpecialGroup
			Dim cmd As New SqlCommand("SELECT TOP 1 * FROM vSpecialGroups WHERE GroupID=@GroupID AND SpecialPageID=@SpecialPageID")
			cmd.Parameters.Add("@GroupID", id)
			cmd.Parameters.Add("@SpecialPageID", specialPageID)

			Return GetSpecialGroupByCmd(cmd)
		End Function

		' Retrieve the special group using the given SQL command object
		' return null if no special group found
		Protected Shared Function GetSpecialGroupByCmd(ByVal cmd As SqlCommand) As SpecialGroup
			Dim sg As SpecialGroup = Nothing

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				cmd.Connection = conn
				Dim dr As SqlDataReader = cmd.ExecuteReader()

				Dim ordID As Integer = dr.GetOrdinal("GroupID")
				Dim ordName As Integer = dr.GetOrdinal("Name")

				If dr.Read() Then
					sg = New SpecialGroup(dr.GetInt32(ordID), dr.GetString(ordName))
					Debug.Assert(Not dr.Read())
				End If

				dr.Close()
			Finally
				conn.Close()
			End Try

			Return sg
		End Function
	End Class
End Namespace






